<?php

namespace App\Jobs;

use App\Models\WxAttachment;
use App\Wen\Utils\FileUtils;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class ImageStateChangeJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    protected $images = null;
    protected $state = 1;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($images, $state)
    {
        //
        $this->images = $images;
        $this->state = $state;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        //
        if(_empty_($this->images)){
            return;
        }
        foreach ($this->images as $url) {
            if(_empty_($url)){
                continue;
            }
            $attachment_part = FileUtils::get_attachment_part_from_url($url);
            if ($attachment_part) {

                if($this->state == 1){
                    WxAttachment::where([
                        ['domain', '=', $attachment_part['domain']],['path', '=', $attachment_part['path']],['state', '=', 0]
                    ])->update(['state' => 1]);
                }else if($this->state == 0){
                    WxAttachment::where([
                        ['domain', '=', $attachment_part['domain']],['path', '=', $attachment_part['path']],['state', '=', 1]
                    ])->update(['state' => 0]);
                }

            }
        }
    }
}
